import store from '@/store'
import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const Home = () =>import ('../pages/Index.vue')
const routes = [
  {
    path: '/',
    redirect:'/home'
  },
  {
    path: '/home',
    name: Home,
    component: Home,
    meta: {
      isAuth:true , // 是否需要登录的权限
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/login.vue')
    
  },
  {
    path: '/register',
    component:()=>import('@/views/Dialog/register.vue')
  },
  {
    path: "/searchbicycle",
    component:()=>import('@/views/searchBicycle/SearchBicycle.vue'),              
  },
  {
    path: '/pay',
    beforeEnter: (to, from, next) => {
      // ...  判断有没有token
      if (store.state.token) {
        next()
      } else {
        next('/login')
      }
    },
    component:()=>import('@/views/pay/payView.vue')
  },
  {
    path: '/desc',
    name:'desc',
    component:()=>import('@/views/bicycle/DescBicycle.vue')
  },
  {
    path: "/profile",
    name: "profile",
    beforeEnter: (to, from, next) => {
      if (store.state.token){
        next()    
      } else {
        next('/login')
      }
    },
    component:()=>import('@/views/Profile/profile.vue')
  }
  
]

const router = new VueRouter({
  mode: 'history',
  routes
})



/***
 * 导航守卫
 * 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登录
 *  
 */
// router.beforeEach((to, from, next) => {
//   console.log('路由拦截器:' + to.meta.isAuth);
//   console.log(to);
//   console.log(from);
//   if (to.matched.some(m => m.meta.requireAuth)) {    // 需要登录
//     if(window.localStorage.token && window.localStorage.isLogin === '1'){
//       next()
//     } else if (to.path !== '/login') {
//       let token = window.localStorage.token;
//       if (token === 'null' || token === '' || token === undefined){
//           next({path: '/login'})
//           console.log('没有登录');
//       }
//     } else {
//       next()
//     }
//   } else {   // 不需要登录
//     next()
//   }

//   // if (to.path === '/pay') {
    
//   // }
//   // if (to.path === '/login') {
//   //   next()
//   // } else {
//   //   let token = localStorage.getItem('token')

//   //   if (token === null || token === '') {
//   //     next('/login')
//   //   } else {
//   //     next()
//   //   }
//   // }
//  })


export default router
